package main

import (
	"fmt"
	. "local/algorithm/util"
)

/**
https://www.youtube.com/watch?v=YZ7Lm8AKwPs
*/
func main() {
	t := generateBst([]int{})
	list := bstToLinkedList(t)
	fmt.Println(list)
}

func bstToLinkedList(root *TreeNode) *TreeNode {
	dn := TreeNode{Right: root}
	prev := dn

	var helper func(root *TreeNode)
	helper = func(root *TreeNode) {
		helper(root.Left)
		prev.Right = root
		root.Left = prev
		prev = root
		helper(root.Right)
	}
	helper(root)
	prev.Right = dn.Right
	dn.Right.Left = prev

	return dn.Right
}

func generateBst(arr []int) *TreeNode {
	//[5,3,6,2,4,null,7]
	root := &TreeNode{Val: 5}
	root.Left = &TreeNode{Val: 3}
	root.Right = &TreeNode{Val: 6}
	root.Left.Left = &TreeNode{Val: 2}
	root.Left.Right = &TreeNode{Val: 4}
	return root
}
